<template>
  <div class="main">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <div class="search">
        <el-form-item label="报告名称" prop="casePlanName">
          <el-input v-model.trim="queryParams.casePlanName" placeholder="请输入項目名称搜索"/>
        </el-form-item>
        <el-form-item label="创建人" prop="createUser">
          <el-input v-model.trim="queryParams.createUser" placeholder="请输入创建人名称搜索"/>
        </el-form-item>
        <el-form-item label="执行结果" prop="result">
          <el-select v-model="queryParams.result" placeholder="请选择执行结果" clearable>
            <el-option v-for="dict in resultList" :key="dict.value" :label="dict.text"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="result">
          <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
            <el-option v-for="dict in typeList" :key="dict.value" :label="dict.text"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
      </div>
      <div class="search">
        <el-button type="primary" @click="restList(query.keywords)" icon="el-icon-search">查询</el-button>
        <el-button @click="refresh('queryForm')" icon="el-icon-refresh">重置</el-button>
      </div>
    </el-form>
    <div class="create-btn">
      <el-table :data="tableData" element-loading-spinner="el-icon-loading" element-loading-text="加载中。。。"
                element-loading-custom-class="equipment-loading" element-loading-background="rgba(55,55,55,0.2)"
                max-height="550">
        <el-table-column fixed prop="casePlanName" label="报告名称">
          <template slot-scope="scope">
            <span class="casePlanName" @click="openReport(scope.row)">{{ scope.row.casePlanName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.type === 1">计划</span>
            <span v-else>用例</span>
          </template>
        </el-table-column>
        <el-table-column prop="driveTime" label="数据驱动次数" width="180">
          <template slot-scope="scope">
            <span v-if="scope.row.driveTime > 0">第{{scope.row.driveTime}}次</span>
            <span v-else> - </span>
          </template>
        </el-table-column>
        <el-table-column prop="result" label="结果" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.result === 1" style="color: #67c23a">成功</span>
            <span v-else style="color: red">失败</span>
          </template>
        </el-table-column>
        <el-table-column prop="createUser" label="执行人" width="150"></el-table-column>
        <el-table-column prop="createTime" label="报告时间" width="200"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <!--            <el-button @click="openDialog('编辑项目',scope.row)" type="text">编辑</el-button>-->
            <el-popconfirm
              @onConfirm="handleDelete(scope.row)"
              confirm-button-text='确定'
              cancel-button-text='我再看看'
              icon="el-icon-info"
              icon-color="red"
              title="是否确认删除？"
            >
              <el-button slot="reference" type="text" v-permission="'record:delete'">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="query.pageNum"
        :limit.sync="query.pageRow"
        layout="total, prev, pager, next"
        :hide-on-single-page="value"
        @pagination="getRecordList"
      />
    </div>
    <report-dialog v-if="reportVisible" :dialogVisble="reportVisible" @closeDialog="closeDialog"
                   :respData="respData" :reportType="reportType"></report-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination/index";
import ReportDialog from "../compoents/reportDialog";

export default {
  name: "record",
  components: {Pagination, ReportDialog},
  data() {
    return {
      queryParams: {
        createUser: "",
        casePlanName: "",
        result: "",
        type: "",
      },
      resultList: [
        {
          text: "成功",
          value: 1
        },
        {
          text: "失败",
          value: 2
        },
      ],
      typeList: [
        {
          text: "计划",
          value: 1
        },
        {
          text: "用例",
          value: 2
        },
      ],
      tableData: [],
      query: {
        pageRow: 10,
        pageNum: 1
      },
      value: false,
      total: 0,
      visible: false,
      reportVisible: false,
      respData: {},
      reportType: "",
    };
  },
  mounted() {
    this.getRecordList();
  },
  methods: {
    openReport(data) {
      this.respData = JSON.parse(data.report)
      this.reportType = "" + data.type
      this.reportVisible = true
    },
    closeDialog() {
      this.reportVisible = false;
    },
    refresh(queryForm) {
      this.$refs[queryForm].resetFields();
      this.queryParams = {
        createUser: "",
        casePlanName: "",
        result: "",
        type: "",
      }
      this.getRecordList();
    },
    restList() {
      this.query.pageNum = 1;
      this.getRecordList();
    },
    getRecordList() {
      this.query = {...this.query, ...this.queryParams}
      this.query["projectId"] = sessionStorage.getItem("projectId")
      this.api({
        url: "/getRecordList",
        method: "post",
        data: this.query
      }).then(resp => {
        this.tableData = resp.list;
        this.total = resp.totalCount;
      });
    },
    handleDelete(data) {
      this.api({url: `/deleteRecord/${data.id}`, method: "delete"}).then((resp) => {
        this.getRecordList()
        this.$message({message: resp, type: 'success'});
      })
    },
  }
};
</script>

<style scoped lang="scss">
.main {
  height: calc(100vh - 110px);
  padding: 20px;
  background-color: white;
  margin: 20px;
}

.casePlanName {
  color: #0044f0;
  cursor: pointer;
}

.search {
  margin: auto;
  text-align: center
}

.create-btn {
  margin-top: 10px;
}
</style>
